package cn.agiledata.security.db.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ConvertUtil;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.MD5;
import cn.agiledata.security.db.form.Md5Form;

import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;

public class Md5TestAction extends IPBBaseAction implements Constant {

	public int hasAccessPermission(int accessLevel, HttpServletRequest request) throws IPBException {
		// TODO Auto-generated method stub
		return 1;
	}

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward md5testCommon(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		Md5Form theForm = (Md5Form)form;
		MD5 md5 = new MD5();
		theForm.setEncryptCode(theForm.getEncryptCode()+"\n"+md5.getMD5ofStr(theForm.getCode()+STRING_SEMICOLON));
	

		theForm.setEncryptCodeSize(String.valueOf(theForm.getEncryptCode().length()));
		
		return mapping.findForward(SUCCESS);
	}
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward md5testLong(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		Md5Form theForm = (Md5Form)form;
		MD5 md5 = new MD5();
		Long d = Long.valueOf(theForm.getCode());
		theForm.setEncryptCode(theForm.getEncryptCode()+"\n"+md5.getMD5ofStr(ConvertUtil.parseDec(d,NUMBER_FORMAT)+STRING_SEMICOLON));
	

		theForm.setEncryptCodeSize(String.valueOf(theForm.getEncryptCode().length()));
		
		return mapping.findForward(SUCCESS);
	}
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward md5testDouble(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		Md5Form theForm = (Md5Form)form;
		MD5 md5 = new MD5();
		Double d = Double.valueOf(theForm.getCode());
		theForm.setEncryptCode(theForm.getEncryptCode()+"\n"+md5.getMD5ofStr(ConvertUtil.parseDec(d,NUMBER_FORMAT)+STRING_SEMICOLON));
	

		theForm.setEncryptCodeSize(String.valueOf(theForm.getEncryptCode().length()));
		
		return mapping.findForward(SUCCESS);
	}
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward md5testDate(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		Md5Form theForm = (Md5Form)form;
		MD5 md5 = new MD5();
		Date date = DateUtil.string2Date(theForm.getCode(),DATE_FORMAT);
		
		theForm.setEncryptCode(theForm.getEncryptCode()+"\n"+md5.getMD5ofStr(DateUtil.date2String(date,DATE_FORMAT)+STRING_SEMICOLON));
	

		theForm.setEncryptCodeSize(String.valueOf(theForm.getEncryptCode().length()));
		
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 重新测试
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward retest(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Md5Form theForm = (Md5Form)form;
		theForm.setCode("");
		theForm.setEncryptCode("");
		theForm.setCodeSize("0");
		theForm.setEncryptCodeSize("0");
		return mapping.findForward(SUCCESS);
	}
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward count(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		Md5Form theForm = (Md5Form)form;
		String str = theForm.getCode();
		String count = String.valueOf(str.length());
		theForm.setCodeSize(count);
		return mapping.findForward(SUCCESS);
	}

}
